<template>
  <el-row>
    <el-col :span="14" class="left">
      <!-- <img src="@/assets/images/logo1.jpg" alt="" /> -->
    </el-col>
    <el-col style="margin: 13% auto" :span="5" :offset="1">
      <el-form
        :model="loginForm"
        ref="loginFormRef"
        :rules="loginFormRules"
        v-if="loginForm.isLogin"
        status-icon
      >
        <h2>登录</h2>
        <el-form-item prop="username">
          <el-input
            style="height: 45px"
            v-model="loginForm.username"
            placeholder="账号"
            :prefix-icon="User"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            style="height: 45px"
            v-model="loginForm.password"
            placeholder="密码"
            type="password"
            show-password
            :prefix-icon="Lock"
          >
          </el-input>
        </el-form-item>
        <!-- 滑动验证码 -->
        <!-- 基础效果 -->
        <!-- https://admin.makeit.vip/pages/login -->
        <div style="width: 100%">
          <mi-captcha
            width="400px"
            ref="captcha"
            :logo="logoImg"
            @success="captchaSuccess"
          ></mi-captcha>
        </div>

        <el-form-item class="forget">
          <el-checkbox-group v-model="loginForm.remember">
            <el-checkbox label="记住我" name="type" />
          </el-checkbox-group>
          <a>忘记密码？</a>
        </el-form-item>
        <el-form-item>
          <el-button
            style="width: 100%; height: 40px"
            type="primary"
            @click="loginSubmit"
            :loading="loading"
            >登录</el-button
          >
        </el-form-item>
        <el-form-item class="register">
          <span @click="register">注册 →</span>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>
<script setup>
// 导入图片
import { reactive, ref } from "vue";
import logoImg from "@/assets/images/login-bgc.jpg";
import { User, Lock, View, Hide } from "@element-plus/icons-vue";
import { registerApi, loginApi } from "@/api/user.js";
import ElementPlus from "element-plus";
import { useUserStore } from "@/stores/user.js";
import { useRouter } from "vue-router";
const router = useRouter();

const loading = ref(false);
// 登录数据
const loginForm = reactive({
  username: "jggjgg",
  password: "123456",
  remember: [],
  isLogin: true,
});

const loginFormRef = ref(null);
const loginFormRules = {
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 5, max: 10, message: "长度在 5 到 10 个字符", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    {
      pattern: /^\S{6,15}/,
      message: "请输入6-15位的非空字符",
      trigger: "blur",
    },
  ],
};
const userStore = useUserStore();
const captchaFlag = ref(false);
// 验证码校验成功回调函数
const captchaSuccess = () => {
  console.log("验证成功");
  captchaFlag.value = true;
};
const loginSubmit = async () => {
  loginFormRef.value.validate(async (valid, fields) => {
    if (captchaFlag.value === false) {
      ElMessage.error("请点击验证码进行校验");
    } else {
      if (valid) {
        loading.value = true;
        const res = await loginApi(loginForm);
        // console.log(res, "res");
        loading.value = false;
        if (res.code == 0) {
          ElMessage.success("登录成功");
          userStore.setToken(res.token);
          loginFormRef.value.resetFields();
          router.replace("/");
        } else {
          ElMessage.error(res.message);
        }
      } else {
        loading.value = false;
      }
    }
  });
};
</script>
<style lang="less" scoped>
/deep/.el-form-item__content {
  display: flex;
  align-items: center;
  justify-content: space-between !important;
}
.left {
  height: 100vh;
  width: 100vw;
  border-radius: 0 20px 20px 0;
  background: url("../../assets/images/login-bgc.jpg") center center / cover
    no-repeat;
  box-sizing: border-box;
}
.forget {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: #409eff;
  margin-top: 10px;
}
.register {
  cursor: pointer;
  color: #409eff;
}
</style>
